//
//  HGOrderCell.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/6.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGOrderCell.h"
#import "HGAddressModel.h"
#import "HGGoodsModel.h"
#import "HGGoodsHeaderView.h"

static CGFloat margin = 14;

@implementation HGOrderCell {
    UILabel *typeLabel;
}

- (void)setType:(NSInteger)type {
    _type = type;
    //1-快递 2-自取 3-平邮
    NSString *content = @"";
    if (type == 1) {
        content = @"快递";
    } else if (type == 2) {
        content = @"自取";
    } else {
        content = @"平邮";
    }
    typeLabel.text = content;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    
    self.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = kColorBackground;
    [self.contentView addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(1);
        make.left.offset(margin);
        make.bottom.right.equalTo(self.contentView);
    }];
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.text = @"配送方式";
    leftLabel.textColor = kColorText;
    leftLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:leftLabel];
    
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.offset(margin);
    }];
    
    
    typeLabel = [[UILabel alloc] init];
    typeLabel.text = @"----";
    typeLabel.textColor = kColorDefult;
    typeLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:typeLabel];
    
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftLabel);
        make.left.equalTo(leftLabel.mas_right).offset(margin/2);
    }];

    UIImageView *rigthImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right_more"]];
    [self.contentView addSubview:rigthImageView];
    
    [rigthImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.offset(-margin);
    }];
}

@end


@implementation HGOrderAddressCell {
    UILabel *_infoLabel;
    UILabel *_adressLabel;
    UIButton *_rigthBtn;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self initSubViews];
    }
    return self;
}

- (void)setModel:(HGAddressModel *)model {
    _model = model;
    _infoLabel.text = [NSString stringWithFormat:@"%@   %@",model.receivePerson,model.receiveTell];
    _adressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",model.province,model.city,model.area,model.detail];
    _rigthBtn.selected = model.isDefault;
}

- (void)setType:(NSInteger)type {
    if (type == 2) {
        _rigthBtn.hidden = YES;
    } else {
        _adressLabel.text = [NSString stringWithFormat:@"收货地址：%@",_model.receiveAddress];
        _rigthBtn.hidden = YES;
    }
}

- (void)initSubViews {
    
    CGFloat margin = 10;
    
    self.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = kColorBackground;
    [self.contentView addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(1);
        make.left.offset(margin);
        make.bottom.right.equalTo(self.contentView);
    }];
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dizhi"]];
    [self.contentView addSubview:leftImageView];
    [leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.offset(margin);
    }];
    
    _infoLabel = [[UILabel alloc] init];
    _infoLabel.text = @"----";
    _infoLabel.textColor = kColorDefult;
    _infoLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_infoLabel];
    
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftImageView.mas_right).offset(margin - 4);
        make.bottom.equalTo(leftImageView.mas_top).offset(margin/2);
    }];
    
    _adressLabel = [[UILabel alloc] init];
    _adressLabel.text = @"----";
    _adressLabel.textColor = kColorDefult;
    _adressLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_adressLabel];
    
    [_adressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_infoLabel.mas_bottom).offset(5);
        make.left.equalTo(_infoLabel);
    }];
    
    _rigthBtn = [[UIButton alloc] init];
    [_rigthBtn setUserInteractionEnabled:NO];
    [_rigthBtn setBackgroundImage:[UIImage imageNamed:@"oder-no"] forState:(UIControlStateNormal)];
    [_rigthBtn setBackgroundImage:[UIImage imageNamed:@"oder"] forState:(UIControlStateSelected)];
    [self.contentView addSubview:_rigthBtn];
    
    [_rigthBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.right.offset(-margin);
    }];
}
@end


@interface HGOrderGoodsCell ()

@property (strong, nonatomic)UIImageView *goodsimage;

@property (strong, nonatomic)UILabel *goodsNameLabel;

@property (strong, nonatomic)UILabel *goodsMoenyLabel;

//加减控件
@property (strong, nonatomic) HGSubtractView *subtractView;

@end

@implementation HGOrderGoodsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubViews];
    }
    return self;
}

- (void)setModel:(HGGoodsModel *)model {
    _model = model;
    [_goodsimage sd_setImageWithURL:kPhotoURL(model.faceImg) placeholderImage:[UIImage imageNamed:@""] options:(SDWebImageProgressiveDownload)];
    _goodsNameLabel.text = [NSString stringWithFormat:@"%@\n6支/箱",model.name];
    [Utility fuwenbenLabel:_goodsNameLabel fontNumber:[UIFont systemFontOfSize:14] andRange:NSMakeRange(_goodsNameLabel.text.length - 4, 4) andColor:kColorDefult];
    _goodsMoenyLabel.text = model.rmbPrice;
    _subtractView.index = model.userChooseCount;
}

- (void)initSubViews {
    
    self.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = kColorBackground;
    [self.contentView addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(1);
        make.left.offset(margin);
        make.bottom.right.equalTo(self.contentView);
    }];
    
    _goodsimage = [[UIImageView alloc] init];
    _goodsimage.backgroundColor = kColorBackground;
    _goodsimage.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_goodsimage];
    
    [_goodsimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.offset(margin);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    _goodsNameLabel = [[UILabel alloc] init];
    _goodsNameLabel.font = [UIFont systemFontOfSize:16];
    _goodsNameLabel.textColor = kColorDefult;
    _goodsNameLabel.numberOfLines = 2;
    _goodsNameLabel.text = @"拉菲庄园至尊红酒\n6支/箱";
    [self.contentView addSubview:_goodsNameLabel];
    
    [_goodsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsimage.mas_right).offset(margin);
        make.top.equalTo(self.goodsimage).offset(10);
    }];
    
    _goodsMoenyLabel = [[UILabel alloc] init];
    _goodsMoenyLabel.font = [UIFont systemFontOfSize:16];
    _goodsMoenyLabel.text = @"￥6888.00";
    _goodsMoenyLabel.textColor = kColorMainTheme;
    [self.contentView addSubview:_goodsMoenyLabel];
    
    [_goodsMoenyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.goodsNameLabel);
        make.top.equalTo(_goodsNameLabel.mas_bottom).offset(margin/2);
    }];
    
    
    _subtractView = [[HGSubtractView alloc] init];
    [self.contentView  addSubview:_subtractView];
    [_subtractView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_goodsimage);
        make.right.offset(-margin);
        make.width.offset(115);
    }];
}

@end

@interface HGGoodsInfoCell ()

//@property (assign, nonatomic) NSInteger payType;

@end

@implementation HGGoodsInfoCell {
    NSMutableArray <UIButton *> *buttons;
    
    NSMutableArray <UILabel*> *rigthLabels;
    
    UILabel *leftLabel;
    
    UIView *lineView;
    UIButton *promptButton;
}

//- (NSInteger)payType {
//    return buttons.firstObject.selected == YES ? 2 : 1;
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubViews];
    }
    return self;
}

- (void)setModel:(HGGoodsModel *)model {
    _model = model;
    [rigthLabels enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.text = idx == 0 ? [NSString stringWithFormat:@"%.2lf",model.userChooseCount * [model.rate floatValue]] : model.userChooseType == 2 ? @"自取" : @"到付";
    }];
    if (model.userChooseType != 2) {
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(35);
        }];
        promptButton.hidden = NO;
    } else {
        [lineView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.offset(1);
        }];
        promptButton.hidden = YES;
    }
}

- (void)initSubViews {
    
    self.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    NSArray *leftTitle = @[@"商品总价",@"运费"];
    rigthLabels = [@[] mutableCopy];
    for (int i = 0; i < leftTitle.count; i++) {
        UILabel *leftLabel = [[UILabel alloc] init];
        leftLabel.font = [UIFont systemFontOfSize:14];
        leftLabel.text = leftTitle[i];
        leftLabel.textColor = kColorText;
        [self.contentView addSubview:leftLabel];
        
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(margin);
            make.top.offset(20 + i *(20 + margin));
        }];
        
        if (i == 1) leftLabel = leftLabel;
        
        
        UILabel *rigthLabel = [[UILabel alloc] init];
        rigthLabel.font = [UIFont systemFontOfSize:14];
        rigthLabel.text = @"---";
        rigthLabel.textColor = kColorMainTheme;
        [self.contentView addSubview:rigthLabel];
        [rigthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftLabel);
            make.right.offset(-margin);
        }];
        
        [rigthLabels addObject:rigthLabel];
    }
    
    lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColorBackground;
    [self.contentView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.subviews[3].mas_bottom).offset(margin);
        make.left.right.equalTo(self.contentView);
        make.height.offset(1);
    }];
    
    promptButton = [[UIButton alloc] init];
    promptButton.hidden = YES;
    [promptButton setTitle:@"《快递公司收费标准》" forState:(UIControlStateNormal)];
    [promptButton setTitleColor:kColorWithFloat(0x09a570) forState:(UIControlStateNormal)];
    promptButton.titleLabel.font = [UIFont systemFontOfSize:12 * kScale];
    [lineView addSubview:promptButton];
    [promptButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(margin);
        make.centerY.equalTo(lineView);
    }];
    
    UILabel *payTypeLabel = [[UILabel alloc] init];
    payTypeLabel.font = [UIFont systemFontOfSize:14];
    payTypeLabel.text = @"请选择支付方式";
    payTypeLabel.textColor = kColorDefult;
    [self.contentView addSubview:payTypeLabel];
    
    [payTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView.mas_bottom).offset(margin);
        make.left.offset(margin);
    }];
    
    
    NSArray *payTitle = @[@"weixin",@"zhifubao"];
    buttons = [@[] mutableCopy];
    for (int i = 0; i < payTitle.count; i++) {
        UIButton *leftBtn = [[UIButton alloc] init];
        [leftBtn setImage:[UIImage imageNamed:payTitle[i]] forState:(UIControlStateNormal)];
        [self.contentView addSubview:leftBtn];
        
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(margin);
            make.top.equalTo(payTypeLabel.mas_bottom).offset(15 + i *(35 + margin));
        }];
        
        UIButton *rigthBtn = [[UIButton alloc] init];
        [rigthBtn setBackgroundImage:[UIImage imageNamed:@"oder-no"] forState:(UIControlStateNormal)];
        [rigthBtn setBackgroundImage:[UIImage imageNamed:@"oder"] forState:(UIControlStateSelected)];
        [rigthBtn addTarget:self action:@selector(changePayTypeAction:) forControlEvents:(UIControlEventTouchUpInside)];
        rigthBtn.selected = i == 0 ? YES : NO;
        [rigthBtn setEnlargeEdgeWithTop:10 right:10 bottom:101 left:10];
        [self.contentView addSubview:rigthBtn];
        [rigthBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(leftBtn);
            make.right.offset(-margin);
        }];
        
        [buttons addObject:rigthBtn];
    }
}

#pragma mark - 支付方式
- (void)changePayTypeAction:(UIButton *)button {
    [buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj == button) {
            obj.selected = YES;
            if (_callBack) {
                _callBack(idx == 0 ? 2 : 1);
            }
            return;
        }
        obj.selected = NO;
    }];
}

@end
